﻿using FocalSpec.FsApiNet.Model;
using NDK.LocalUtils.Yield;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NDK.Frm
{
    public partial class Frm_YieldStatistics : Form
    {
        #region 窗体对象实例，单例模式,保证整个工程只有一个实例
        /// <summary>
        /// 窗体对象实例，单例模式,保证整个工程只有一个实例
        /// </summary>
        private static Frm_YieldStatistics _instance;
        private static readonly object singletonLock = new object();
        public static Frm_YieldStatistics Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (singletonLock)
                    {
                        if (_instance == null)
                        {
                            _instance = new Frm_YieldStatistics();
                        }
                    }
                }
                return _instance;
            }
        }
        #endregion
        /***按钮提示语***/
        private ToolTip toolTip1 = new ToolTip();

        public Frm_YieldStatistics()
        {
            InitializeComponent();
        }

        private void YieldStatistics_Load(object sender, EventArgs e)
        {
            toolTip1.InitialDelay = 200;
            toolTip1.ReshowDelay = 200;
            toolTip1.ShowAlways = true;
            toolTip1.IsBalloon = true;
            Yield.Instance.HourCapacityChangedEvent += Instance_CapacityChangedEvent;//订阅当天良率统计更新事件
            Yield.Instance.ClassesCapacityChangedEvent += Instance_ClassesChangedEvent;//订阅一周良率统计更新实践

            Yield.Instance.SearchHourCapacityEvent += Instance_SearchHourCapacityEvent;//查询当天良率统计更新事件
            Yield.Instance.SearchClassesCapacityEvent += Instance_SearchClassesCapacityEvent;//查询最近一周良率统计
        }
        /// <summary>
        /// 语言改变事件订阅方法
        /// </summary>
        /// <param name="strLanguage"></param>
        private void Instance_ChangeLanguageHandle(string strLanguage)
        {
            #region 按钮提示语
            toolTip1.SetToolTip(btn_Write, "交接班时间写入");
            #endregion

            /***检索当前时间是否在白班的时间段内***/
            string classes = Yield.Instance.GetCurClasses();
            if (classes == "Day")
            {
                /***白班***/
                chart_Statist.Titles[0].Text = "白班统计";
                chart_Week.Titles[0].Text = "白班一周良率统计";
                chart_Yield.Titles[0].Text = "白班良率";

            }
            else
            {
                /***夜班***/

                chart_Statist.Titles[0].Text = "夜班统计";
                chart_Week.Titles[0].Text = "夜班一周良率统计";
                chart_Yield.Titles[0].Text = "夜班良率";

            }

            chart_Statist.Series[0].LegendText = "总数";
            chart_Statist.Series[1].LegendText = "OK数";
            chart_Statist.Series[2].LegendText = "NG数";
            chart_Statist.Series[3].LegendText = "良率";
            chart_Week.Series[0].LegendText = "总数";
            chart_Week.Series[1].LegendText = "OK数";
            chart_Week.Series[2].LegendText = "NG数";
            chart_Week.Series[3].LegendText = "良率";
        }
        private void Instance_ClassesChangedEvent(object sender, Dictionary<string, ClassesCapacity> e)
        {
            this.Invoke(new Action(() =>
            {
                List<int> lsTotal = new List<int>();
                List<int> lsNG = new List<int>();
                List<int> lsOK = new List<int>();
                List<double> lsScale = new List<double>();
                foreach (KeyValuePair<string, ClassesCapacity> kvp in e)
                {
                    lsTotal.Add(kvp.Value.TotalCount);
                    lsNG.Add(kvp.Value.NgCount);
                    lsOK.Add(kvp.Value.OkCount);
                    if (kvp.Value.TotalCount == 0)
                    {
                        lsScale.Add(100);
                    }
                    else
                    {
                        double dd = Math.Round((double)kvp.Value.OkCount / kvp.Value.TotalCount, 3);
                        lsScale.Add(dd * 100);
                    }
                }
                chart_Week.Series[0].Points.DataBindXY(e.Keys.ToList(), lsTotal);
                chart_Week.Series[1].Points.DataBindXY(e.Keys.ToList(), lsOK);
                chart_Week.Series[2].Points.DataBindXY(e.Keys.ToList(), lsNG);
                chart_Week.Series[3].Points.DataBindXY(e.Keys.ToList(), lsScale);
            }));
        }

        private void Instance_CapacityChangedEvent(object sender, Dictionary<string, HourCapacity> e)
        {
            this.Invoke(new Action(() =>
            {
                List<int> lsTotal = new List<int>();
                List<int> lsNG = new List<int>();
                List<int> lsOK = new List<int>();
                List<double> lsScale = new List<double>();
                foreach (KeyValuePair<string, HourCapacity> kvp in e)
                {
                    lsTotal.Add(kvp.Value.TotalCount);
                    lsNG.Add(kvp.Value.NgCount);
                    lsOK.Add(kvp.Value.OkCount);
                    if (kvp.Value.TotalCount == 0)
                    {
                        lsScale.Add(100);
                    }
                    else
                    {
                        double dd = Math.Round((double)kvp.Value.OkCount / kvp.Value.TotalCount, 3);
                        lsScale.Add(dd * 100);
                    }
                }
                chart_Statist.Series[0].Points.DataBindXY(e.Keys.ToList(), lsTotal);
                chart_Statist.Series[1].Points.DataBindXY(e.Keys.ToList(), lsOK);
                chart_Statist.Series[2].Points.DataBindXY(e.Keys.ToList(), lsNG);
                chart_Statist.Series[3].Points.DataBindXY(e.Keys.ToList(), lsScale);
                Dictionary<string, int> dc = new Dictionary<string, int>();
                dc.Add("OK", lsOK.Sum());
                dc.Add("NG", lsNG.Sum());
                chart_Yield.Series[0].Points.DataBindXY(dc.Keys.ToList(), dc.Values.ToList());
                chart_Yield.Series[0].Points[0].Color = Color.LimeGreen;
                chart_Yield.Series[0].Points[0].LabelForeColor = Color.LimeGreen;
                chart_Yield.Series[0].Points[1].Color = Color.Red;
                chart_Yield.Series[0].Points[1].LabelForeColor = Color.Red;
            }));
        }

        private void Instance_SearchHourCapacityEvent(object sender, Dictionary<string, HourCapacity> e)
        {
            this.Invoke(new Action(() =>
            {
                List<int> lsTotal = new List<int>();
                List<int> lsNG = new List<int>();
                List<int> lsOK = new List<int>();
                List<double> lsScale = new List<double>();
                foreach (KeyValuePair<string, HourCapacity> kvp in e)
                {
                    lsTotal.Add(kvp.Value.TotalCount);
                    lsNG.Add(kvp.Value.NgCount);
                    lsOK.Add(kvp.Value.OkCount);
                    if (kvp.Value.TotalCount == 0)
                    {
                        lsScale.Add(100);
                    }
                    else
                    {
                        double dd = Math.Round((double)kvp.Value.OkCount / kvp.Value.TotalCount, 3);
                        lsScale.Add(dd * 100);
                    }
                }
                chart_Statist.Series[0].Points.DataBindXY(e.Keys.ToList(), lsTotal);
                chart_Statist.Series[1].Points.DataBindXY(e.Keys.ToList(), lsOK);
                chart_Statist.Series[2].Points.DataBindXY(e.Keys.ToList(), lsNG);
                chart_Statist.Series[3].Points.DataBindXY(e.Keys.ToList(), lsScale);
                Dictionary<string, int> dc = new Dictionary<string, int>();
                dc.Add("OK", lsOK.Sum());
                dc.Add("NG", lsNG.Sum());
                chart_Yield.Series[0].Points.DataBindXY(dc.Keys.ToList(), dc.Values.ToList());
                chart_Yield.Series[0].Points[0].Color = Color.LimeGreen;
                chart_Yield.Series[0].Points[0].LabelForeColor = Color.LimeGreen;
                chart_Yield.Series[0].Points[1].Color = Color.Red;
                chart_Yield.Series[0].Points[1].LabelForeColor = Color.Red;
            }));
        }


        private void Instance_SearchClassesCapacityEvent(object sender, Dictionary<string, ClassesCapacity> e)
        {
            this.Invoke(new Action(() =>
            {
                List<int> lsTotal = new List<int>();
                List<int> lsNG = new List<int>();
                List<int> lsOK = new List<int>();
                List<double> lsScale = new List<double>();
                foreach (KeyValuePair<string, ClassesCapacity> kvp in e)
                {
                    lsTotal.Add(kvp.Value.TotalCount);
                    lsNG.Add(kvp.Value.NgCount);
                    lsOK.Add(kvp.Value.OkCount);
                    if (kvp.Value.TotalCount == 0)
                    {
                        lsScale.Add(100);
                    }
                    else
                    {
                        double dd = Math.Round((double)kvp.Value.OkCount / kvp.Value.TotalCount, 3);
                        lsScale.Add(dd * 100);
                    }
                }
                chart_Week.Series[0].Points.DataBindXY(e.Keys.ToList(), lsTotal);
                chart_Week.Series[1].Points.DataBindXY(e.Keys.ToList(), lsOK);
                chart_Week.Series[2].Points.DataBindXY(e.Keys.ToList(), lsNG);
                chart_Week.Series[3].Points.DataBindXY(e.Keys.ToList(), lsScale);
            }));
        }

        private void txt_HandHour_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < 53 || e.KeyChar > 57) && e.KeyChar != 8)
            {
                MessageBox.Show("请输入5-9范围内数字！", "Warn", MessageBoxButtons.OK, MessageBoxIcon.Error);
                e.Handled = true;
            }
        }

        private void txt_HnadMin_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
            {
                MessageBox.Show("请输入合法数字！", "Warn", MessageBoxButtons.OK, MessageBoxIcon.Error);
                e.Handled = true;
            }
        }

        private void txt_HandHour_TextChanged(object sender, EventArgs e)
        {
            if (txt_HandHour.Text != "")
            {
                int number = int.Parse(txt_HandHour.Text);
                txt_HandHour.Text = number.ToString();
                if (number > 9 || number < 5)
                {
                    MessageBox.Show("请输入5-9范围内数字！", "Warn", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txt_HandHour.Text = txt_HandHour.Text.Remove(0);
                    txt_HandHour.SelectionStart = 0;
                    return;
                }
            }
        }


        private void txt_HnadMin_TextChanged(object sender, EventArgs e)
        {
            if (txt_HnadMin.Text != "")
            {
                int number = int.Parse(txt_HnadMin.Text);
                txt_HnadMin.Text = number.ToString();
                if (number > 59)
                {

                    MessageBox.Show("请输入0-59范围内数字！", "Warn", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txt_HnadMin.Text = txt_HnadMin.Text.Remove(0);
                    txt_HnadMin.SelectionStart = 0;
                    return;
                }
            }
        }

        private void YieldStatistics_Shown(object sender, EventArgs e)
        {
            /***显示交接时间，分钟***/
            txt_HandHour.Text = Yield.Instance.HandOverHour;
            txt_HnadMin.Text = Yield.Instance.HandOverMinute;
            /***显示白班或者夜班统计信息***/
            Yield.Instance.ShowClassesYield();
        }

        private void btn_Write_Click(object sender, EventArgs e)
        {
            if (txt_HandHour.Text.Trim() != "" &&
                txt_HnadMin.Text.Trim() != "")
            {
                if (txt_HandHour.Text.Trim().PadLeft(2, '0') == Yield.Instance.HandOverHour.ToString()
                    && txt_HnadMin.Text.Trim().PadLeft(2, '0') == Yield.Instance.HandOverMinute.ToString())
                {
                    return;
                }
                if (Yield.Instance.SetHandOverTime(txt_HandHour.Text.Trim().PadLeft(2, '0'), txt_HnadMin.Text.Trim().PadLeft(2, '0')))
                {
                    MessageBox.Show("交接班时间设定成功！", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                else
                {
                    MessageBox.Show("交接班时间设定失败！", "Warn", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }
            }
            else
            {
                MessageBox.Show("设定值不可以为空！", "Warn", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            
        }

        private void btn_Query_Click(object sender, EventArgs e)
        {
            DateTime data = dateTimePicker1.Value;
            string calsses = cmb_Select.Text.Trim();
            if (Yield.Instance.SearchClassesYield(data, calsses))
            {
                chart_Statist.Titles[0].Text = dateTimePicker1.Value.ToString("yyyy-MM-dd") + "-" + calsses;
                chart_Week.Titles[0].Text = dateTimePicker1.Value.ToString("yyyy-MM-dd") + "-" + calsses + "-" + "Week";
                chart_Yield.Titles[0].Text = dateTimePicker1.Value.ToString("yyyy-MM-dd") + "-" + calsses;
            }
        }
    }
}
